package mahan.test.data.mongodb;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Date;

import mahan.data.model.Customer;
import mahan.data.mongodb.MahanDB;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class CustomerTestCase {

	private MahanDB mahanDb;

	@After
	public void clean() {
		mahanDb.get().dropDatabase();
	}

	@Before
	public void setup() {
		mahanDb = new MahanDB("localhost", 27017, "custdb");
		DB db = mahanDb.get();
		assertNotNull(db);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testCust() throws Exception {
		clean();
		Date date = new Date();
		Customer cust = new Customer(1, date);
		cust.setAddress("123 c");
		cust.setAdmin(true);
		cust.setCity("c");
		cust.setCountry("zogata");
		cust.setEmailAddress("a@a.com");
		cust.setFacebookID("some@some");
		cust.setFirstName("C");
		cust.setLastName("N");
		cust.setLastPurchase(date);
		cust.setLastVisit(date);
		cust.setPhoneNumber("111111");
		cust.setState("ZX");
		cust.setTwitterID("some");

		// Store into DB
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(cust.get());
		DBCollection coll = mahanDb.createCollection("cust");
		coll.save(dbo);

		// Retrieve from DB
		DBObject fromDB = coll.findOne();
		Customer from = Customer.instance();
		from.put(fromDB.toMap());

		assertEquals("123 c", from.getAddress());
		assertEquals(true, from.isAdmin());
		assertEquals("c", from.getCity());
		assertEquals("zogata", from.getCountry());
		assertEquals("a@a.com", from.getEmailAddress());
		assertEquals("some@some", from.getFacebookID());
		assertEquals("C", from.getFirstName());
		assertEquals("N", from.getLastName());
		assertEquals(date, from.getLastPurchase());
		assertEquals(date, from.getLastVisit());
		assertEquals("111111", from.getPhoneNumber());
		assertEquals("ZX", from.getState());
		assertEquals("some", from.getTwitterID());
		assertEquals(date, from.getCreatedOn());
	}
}